// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * Given a map whose keys are output files and whose values are the flame.Action
 * objects that output them, create a dependency graph of files. The nodes are
 * every output file and every input file that is involed in creating an output,
 * and the edges are (output, input) dependency pairs.
 *
 * @param {!flame.internal.StringMap} outputFileIndex
 * @return {!flame.internal.DirectedAcyclicGraph}
 */
flame.internal.buildFileGraph = function(outputFileIndex) {
  var fi = flame.internal;

  /** @type {!Array.<flame.internal.GraphEdge>} */
  var edges = [];
  var nodes = new fi.StringSet;

  outputFileIndex.getKeys().forEach(function(output) {
    var action = outputFileIndex.get(output);
    var inputs = action.inputs;

    nodes.insert(output);
    inputs.forEach(function(input) {
      nodes.insert(input);
      edges.push(new fi.GraphEdge(output, input));
    });
  });

  return new fi.DirectedAcyclicGraph(nodes, edges);
};
